document.addEventListener('DOMContentLoaded', function() {
    // 登录页面逻辑
    if (document.getElementById('loginBtn')) {
        const loginBtn = document.getElementById('loginBtn');
        loginBtn.addEventListener('click', handleLogin);
    }

    // 注册页面逻辑
    if (document.getElementById('registerBtn')) {
        const registerBtn = document.getElementById('registerBtn');
        registerBtn.addEventListener('click', handleRegister);
    }
});

function handleLogin() {
    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;
    const rememberMe = document.getElementById('rememberMe').checked;

    if (!username || !password) {
        showAlert('请输入用户名和密码', 'error');
        return;
    }

    const loginBtn = document.getElementById('loginBtn');
    const originalText = loginBtn.textContent;
    loginBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 登录中...';
    loginBtn.disabled = true;

    api.login({ username, password })
        .then(data => {
            if (data.code === 200) {
                showAlert('登录成功', 'success');
                localStorage.setItem('user', JSON.stringify(data.data));
                setTimeout(() => {
                    window.location.href = '/';
                }, 1500);
            } else {
                showAlert(data.message || '登录失败', 'error');
                loginBtn.textContent = originalText;
                loginBtn.disabled = false;
            }
        })
        .catch(error => {
            showAlert('登录失败：' + error.message, 'error');
            loginBtn.textContent = originalText;
            loginBtn.disabled = false;
        });
}

function handleRegister() {
    const username = document.getElementById('regUsername').value;
    const password = document.getElementById('regPassword').value;
    const confirmPassword = document.getElementById('regConfirmPassword').value;
    const phone = document.getElementById('regPhone').value;
    const agreement = document.getElementById('agreement').checked;

    if (!username) {
        showAlert('请输入用户名', 'error');
        return;
    }

    if (!password) {
        showAlert('请输入密码', 'error');
        return;
    }

    if (password.length < 6) {
        showAlert('密码长度不能少于6位', 'error');
        return;
    }

    if (password !== confirmPassword) {
        showAlert('两次输入的密码不一致', 'error');
        return;
    }

    if (!validate.phone(phone)) {
        showAlert('请输入正确的手机号码', 'error');
        return;
    }

    if (!agreement) {
        showAlert('请阅读并同意用户协议和隐私政策', 'error');
        return;
    }

    const registerBtn = document.getElementById('registerBtn');
    const originalText = registerBtn.textContent;
    registerBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 注册中...';
    registerBtn.disabled = true;

    api.register({ username, password, phone })
        .then(data => {
            if (data.code === 200) {
                showAlert('注册成功', 'success');
                setTimeout(() => {
                    window.location.href = 'login.html';
                }, 1500);
            } else {
                showAlert(data.message || '注册失败', 'error');
                registerBtn.textContent = originalText;
                registerBtn.disabled = false;
            }
        })
        .catch(error => {
            showAlert('注册失败：' + error.message, 'error');
            registerBtn.textContent = originalText;
            registerBtn.disabled = false;
        });
}